package org.example;

import org.example.loadBalancers.LoadBalancer;

import java.util.LinkedHashSet;

public class MyLoadBalancer extends LoadBalancer {


    public MyLoadBalancer() {
        super("my");
    }



    //自定义 随机算法
    @Override
    public ServiceInstance getServiceInstance(String serviceName) {
        LinkedHashSet<ServiceInstance> serviceInstances = getServiceInstances(serviceName);
        // 如果没有可用的服务实例，抛出异常或返回 null
        if (serviceInstances.isEmpty()) {
            return null;
        }

        //随机获取一个服务实例
        int index = (int) (Math.random() * serviceInstances.size());
        return (ServiceInstance) serviceInstances.toArray()[index];
    }
}
